﻿<?php
Class SafePDO extends PDO {

	public static function exception_handler($exception) {
		// Output the exception details
		die('Uncaught exception: '.$exception->getMessage());
	}

	public function __construct($dsn, $username='', $password='', $driver_options=array()) {

		// Temporarily change the PHP exception handler while we . . .
		set_exception_handler(array(__CLASS__, 'exception_handler'));

		// . . . create a PDO object
		parent::__construct($dsn, $username, $password, $driver_options);

		// Change the exception handler back to whatever it was before
		restore_exception_handler();
	}
	
	public function getAllUsers(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_user, name, pass, login FROM `users_bliss`");
		if($stmt->execute()) {
			while ($row = $stmt->fetch()) {
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getUserByLogin($login, $pass = NULL, $id=NULL){
		$ret = 0;
		if($pass != NULL){
			$stmt = $this->prepare("SELECT id_user FROM `users_bliss` WHERE login=:login AND pass=:pass");
			$stmt->bindParam(':login', $login);
			$pass = sha1($pass);
			$stmt->bindParam(':pass', $pass);
			if($stmt->execute()) {
				$row = $stmt->fetch();
				$ret=$row['id_user'];
			}
		}elseif($id != NULL){
			$stmt = $this->prepare("SELECT id_user FROM `users_bliss` WHERE login=:login AND id_user<>:id_user");
			$stmt->bindParam(':login', $login);
			$stmt->bindParam(':id_user', $id);
			if($stmt->execute()) {
				$row = $stmt->fetch();
				if(isset($row['id_user'])){
					$ret=$row['id_user'];
				}
			}
		}
		else{
			$stmt = $this->prepare("SELECT id_user FROM `users_bliss` WHERE login=:login");
			$stmt->bindParam(':login', $login);
			if($stmt->execute()) {
				$row = $stmt->fetch();
				$ret=$row['id_user'];
			}
		}
		if($ret == NULL)
			$ret = 0;
		return $ret;
	}
	
	public function getMachineByIP($IP, $id=NULL){
		$ret = 0;
	
		if($id != NULL){
			$stmt = $this->prepare("SELECT id_machine FROM `machine_bliss` WHERE IP=:IP AND id_machine<>:id_machine");
			$stmt->bindParam(':IP',$IP);
			$stmt->bindParam(':id_machine',$id);
			if($stmt->execute()){
				$row = $stmt->fetch();
				$ret=$row['id_machine'];
			} 
		}else{
			$stmt = $this->prepare("SELECT id_machine FROM `machine_bliss` WHERE IP=:IP");
			$stmt->bindParam(':IP',$IP);
			if($stmt->execute()){
				$row = $stmt->fetch();
				$ret=$row['id_machine'];
			}
		}
		if($ret == NULL)
			$ret = 0;
		return $ret;
	}
	
	public function getAllIncident(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_incident, machine, type, complement, date_detection, date_resolution FROM `incident_bliss`");
		if($stmt->execute()){
			while ($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllSalles(){
		$ret=array();
		$stmt=$this->prepare("SELECT id_salle, nom_salle FROM `salle_bliss`");
		if($stmt->execute()){
			while ($row=$stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllType_Incident(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_type_incident, nom_type FROM `type_incident_bliss`");
		if($stmt->execute()){
			while($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllMachine(){
		$ret = array();
		$stmt = $this->prepare("SELECT id_machine, IP, nom, salle FROM `machine_bliss`");
		if($stmt->execute()){
			while($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllMachineBySalle($id_salle){
		$ret = array();
		$stmt = $this->prepare("SELECT id_machine, IP, nom, salle FROM `machine_bliss` WHERE salle=:id_salle");
		$stmt->bindParam(':id_salle', $id_salle);
		if($stmt->execute()){
			while($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllIncidentByMachine($id_machine){
		$ret = array();
		$stmt = $this->prepare("SELECT id_incident, machine, type, complement, date_detection, date_resolution FROM incident_bliss WHERE machine=:id_machine");
		$stmt->bindParam(':id_machine', $id_machine);
		if($stmt->execute()){
			while ($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}
	
	public function getAllIncidentByType($id_type_incident){
		$ret = array();
		$stmt = $this->prepare("SELECT id_incident, machine, type, complement, date_detection, date_resolution FROM incident_bliss WHERE type=:id_type_incident");
		$stmt->bindParam(':id_type_incident', $id_type_incident);
		if($stmt->execute()){
			while ($row = $stmt->fetch()){
				$ret[]=$row;
			}
		}
		return $ret;
	}

}

define('PDO_DSN','mysql:dbname=bliss;host=localhost');
define('PDO_USER','bliss_user');
define('PDO_PASSWORD','root');

// Connect to the database with defined constants
$dbh = new SafePDO(PDO_DSN, PDO_USER, PDO_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));